package com.agricultural.strategy.impl;

import com.agricultural.equipment.AgriculturalEquipment;
import com.agricultural.strategy.PathPlanningStrategy;

import java.util.Map;

// 避障路径规划策略 - 当检测到障碍物时使用
public class ObstacleAvoidanceStrategy implements PathPlanningStrategy {
    
    // 获取策略名称
    @Override
    public String getStrategyName() {
        return "避障路径规划策略";
    }
    
    // 规划路径
    @Override
    public void planPath(AgriculturalEquipment equipment, Map<String, Object> environmentData) {
        System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 使用避障路径规划策略");
        
        // 检查环境数据中是否有障碍物信息
        double obstacleDistance = Double.MAX_VALUE; // 默认值
        if (environmentData != null && environmentData.containsKey("obstacleDistance")) {
            obstacleDistance = (double) environmentData.get("obstacleDistance");
        }
        
        System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 检测到障碍物，距离: " + obstacleDistance + "米");
        
        // 模拟路径规划计算 - 避开障碍物
        double currentX = equipment.getStatus().get("positionX") != null ? 
                          (double) equipment.getStatus().get("positionX") : 0.0;
        double currentY = equipment.getStatus().get("positionY") != null ? 
                          (double) equipment.getStatus().get("positionY") : 0.0;
        
        // 根据障碍物距离决定如何避开
        double newX, newY;
        if (obstacleDistance < 5.0) {
            System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 紧急避障！");
            newX = currentX - 5.0; // 后退
            newY = currentY + 8.0; // 横向移动
        } else if (obstacleDistance < 10.0) {
            System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 准备避障");
            newX = currentX;
            newY = currentY + 5.0; // 横向移动
        } else {
            System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 障碍物距离较远，保持当前路径");
            newX = currentX + 10.0;
            newY = currentY;
        }
        
        // 更新设备位置
        String newPosition = String.format("(%.2f, %.2f)", newX, newY);
        equipment.updateStatusData("position", newPosition);
        
        System.out.println("[" + equipment.getType() + "-" + equipment.getId() + "] 新路径规划完成: " + newPosition);
    }
}    